import os
from PIL import Image

def resize_and_rename_images(input_dir, output_dir):
    """
    处理图片：调整尺寸为1024x1024，并按子文件夹名+序号命名
    :param input_dir: 输入目录路径
    :param output_dir: 输出目录路径
    """
    # 确保输出目录存在
    os.makedirs(output_dir, exist_ok=True)
    
    # 遍历输入目录
    for root, dirs, files in os.walk(input_dir):
        # 跳过根目录自身
        if root == input_dir:
            continue
        
        # 获取当前子文件夹名称
        folder_name = os.path.basename(root)
        
        # 筛选图片文件
        valid_ext = {'jpg', 'jpeg', 'png', 'gif', 'bmp'}
        image_files = [
            f for f in files 
            if f.split('.')[-1].lower() in valid_ext
        ]
        image_files.sort()  # 按文件名排序
        
        # 处理每个图片
        for index, filename in enumerate(image_files, 1):
            # 构建完整路径
            src_path = os.path.join(root, filename)
            
            # 生成新文件名
            ext = filename.split('.')[-1].lower()
            new_name = f"{folder_name}_{index:03d}.{ext}"
            dst_path = os.path.join(output_dir, folder_name)
            img_path = os.path.join(dst_path, new_name)

            if not os.path.exists(dst_path):
                os.mkdir(dst_path)
            
            try:
                # 打开并处理图片
                with Image.open(src_path) as img:
                    # 调整尺寸
                    img = img.resize((1024, 1024))
                    # 处理透明通道
                    if img.mode in ('RGBA', 'LA'):
                        img = img.convert('RGB')
                    # 保存图片
                    img.save(img_path)
                    print(f"处理成功：{src_path} -> {img_path}")
            except Exception as e:
                print(f"处理失败：{src_path} | 错误：{str(e)}")

if __name__ == "__main__":
    input_dir = 'D:\梦工厂第一组'
    output_dir = './data'
    resize_and_rename_images(input_dir, output_dir)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

















































